package com.xszx.controller;

import com.xszx.bean.Card;
import com.xszx.bean.User;
import com.xszx.dao.CardDao;
import com.xszx.dao.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
@Transactional
public class MyController {
    @Autowired
    private UserDao userDao;
    @Autowired
    private CardDao cardDao;
    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(@RequestBody Map map){

        User user = new User(map.get("name").toString(),map.get("phone").toString(),map.get("address").toString());
        userDao.save(user);
        Map map1 = new HashMap();
        map1.put("result",true);
        map1.put("msg","成功添加用户");
        return map1;
    }
    @RequestMapping("/addCard")
    @ResponseBody
    public Map addCard(@RequestBody Map map){
        String phone=map.get("phone").toString();
        User user=userDao.findByPhone(phone);
        Map map1 = new HashMap();
        if(user==null){
            map1.put("result",false);
            map1.put("msg","请先完善信息");
            return map1;
        }
        Card card = new Card(map.get("num").toString(), (int)map.get("password"), map.get("status").toString(), (Date) map.get("time") , (int)map.get("balance") , user );
        cardDao.save(card);

        map1.put("result",true);
        map1.put("msg","开户成功");
        return map1;
    }

}
